package com.zhixi.datasource;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author zhixi
 * Aop切面，对注解进行切面
 */
@Aspect
@Component
public class DataSourceAspect {

    @Pointcut("@annotation(com.zhixi.datasource.UsingDataSource)")
    public void checkPointCut() {

    }

    /**
     * 方法调用之前设置数据源
     * @param usingDataSource 拿到注解的数据源的值
     */
    @Before("checkPointCut() && @annotation(usingDataSource)")
    public void checkBefore(UsingDataSource usingDataSource) {
        // 添加数据源的key
        DataSourceContextHolder.setKey(usingDataSource.value());
    }

    @After("checkPointCut()")
    public void checkAfter() {
        DataSourceContextHolder.clearKey();
    }
}
